import Vue from "vue";
import Vuex from "vuex";
import createLogger from "vuex/dist/logger";
import api from '@/api';

Vue.use(Vuex);
let env = process.env.NODE_ENV,
    plugins = [];
if (env === 'development') plugins.push(createLogger());

const store = new Vuex.Store({
    state: {
        profile: null
    },
    mutations: {
        setProfile(state, payload) {
            state.profile = payload;
        }
    },
    actions: {
        async setProfileAsync({ commit }) {
            let result;
            try {
                result = await api.profile();
                if (+result.resultCode !== 200) result.data = null;
                // 模拟获取到了权限
                if (result.data) {
                    result.data.power = "XFP001|XFP002|XFP005|XFP006|XFP007|XFP008";
                }
                commit('setProfile', result.data);
            } catch (_) { }
            return result?.data;
        }
    },
    plugins
});
export default store;